package com.example.demo.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;

/**
 * 拦截所有的空指针异常，进行统一的数据返回
 */
@RestControllerAdvice
public class MyExHandler {



    //空指针异常处理
    @ExceptionHandler(NullPointerException.class)
    public HashMap<String,Object> nullException(NullPointerException e){
        HashMap<String,Object> result=new HashMap<>();
        result.put("code","-1");
        result.put("msg","空指针异常"+e.getMessage());
        result.put("data",null);
        return result;
    }

    //算术异常处理
    @ExceptionHandler(ArithmeticException.class)
    public HashMap<String,Object> ArithmeticException(ArithmeticException e){
        HashMap<String,Object> result=new HashMap<>();
        result.put("code","-1");
        result.put("msg","算数异常"+e.getMessage());
        result.put("data",null);
        return result;
    }

}
